//
//  TPProductDetailViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/25.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPProductDetailViewController.h"
#import "TPProductDetailViewModel.h"
#import "TPDetailInfoCell.h"
#import "TPDetailHospitalCell.h"
#import "TPHomeCell.h"
#import "YPWebViewModel.h"
#import "TPProductWebViewCell.h"
#import "TPDetailFooter.h"
#import "TPPayViewController.h"
#import "TPProductDetailModel.h"
@interface TPProductDetailViewController ()
@property (nonatomic,strong)TPProductDetailViewModel *viewModel;
@property (nonatomic,copy)NSArray *headers;
@property (nonatomic,strong)NSMutableArray *webViewModels;
@property (nonatomic,strong)TPDetailFooter *footer;
@property (nonatomic,strong)TPProductDetailModel *detailModel;
@end

@implementation TPProductDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"产品详情";
    _headers = @[@"",@"",@"产品介绍",@"购买注意事项",@"可能喜欢"];
    [self setup];
}
-(void)setup{
    
    _viewModel = [[TPProductDetailViewModel alloc]init];
    
    
    self.rightBtn.width = 30.0;
    [self.rightBtn setImage:[ImageNamed(@"TP_detail_stars_normal") imageWithGradientTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [self.rightBtn setImage:ImageNamed(@"TP_detail_stars_select") forState:UIControlStateSelected];
    
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50.0, 0);
    self.tableView.backgroundColor = TP_COLOR_245;
    UIImageView *bannarView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 200.0)];
    bannarView.contentMode = UIViewContentModeScaleAspectFill;
    bannarView.clipsToBounds = YES;
    self.tableView.tableHeaderView = bannarView;
    [bannarView ym_setImageWithURL:self.model.goods_image placeholderImage:defaultLogo];
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self webviews];
    
    _footer = [[TPDetailFooter alloc]init];
    [self.view addSubview:_footer];
    [self.view bringSubviewToFront:_footer];
    [_footer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0.0);
        make.height.mas_equalTo(50.0);
    }];
    @weakify(self);
    [_footer setDidClickBlock:^(UIButton *sender) {
        @strongify(self);
        if(sender.tag == 222){ //立即预约
            
            if(!tp_isLogin())
                return;

            TPPayViewController *payVC = [TPPayViewController new];
            payVC.detailModel = self.detailModel;
            [self.navigationController pushViewController:payVC animated:YES];
        }else{ //在线客服
            [XWCommon getPhoneNuber:xw_getNULLString(self.detailModel.clinic.link_mobile) view:self.view];
        }
    }];
    
    [self getRequest];
}

-(void)getRequest{
    
    [[TPRequestClient sharedClient]getTemplateInfotemplateId:self.model.goods_id success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        self.detailModel = [TPProductDetailModel yy_modelWithJSON:respone.data];
        
        self.rightBtn.selected = self.detailModel.is_collection;
        
        YPWebViewModel *model = self.webViewModels.firstObject;
        model.html = self.detailModel.goods_body;
        
        YPWebViewModel *model1 = self.webViewModels.lastObject;
        model1.html = self.detailModel.needing;
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        ;
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        ;
    }];
    
    
    [[TPRequestClient sharedClient]getTemplateListCateId:0 cityName:[XWCommon shareAppDelegate].locationCity goods_salenum:@"" clinic_id:0 name:@"" recommend:true current:self.page size:kMaxRequestSize success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        if(self.page == 1)
            [self.dataSource removeAllObjects];
        NSArray *array = [NSArray yy_modelArrayWithClass:[TPHomeModel class] json:respone.data[@"data"]];
        [self.dataSource addObjectsFromArray:array];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}
-(void)rightBtnAction:(UIButton *)sender{
    
    
    if(!xw_getLogin())
        return;
    
    [[TPRequestClient sharedClient]getCollectionCreateId:self.detailModel.goods_id  isAdd:!sender.isSelected success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        sender.selected = !sender.selected;
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        
        [self.view xw_hideTitle:respone.message];
        
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        ;
    }];
}
-(void)webviews{
    self.webViewModels = [NSMutableArray new];
    [self.webViewModels addObject:[[YPWebViewModel alloc]init]];
    [self.webViewModels addObject:[[YPWebViewModel alloc]init]];
    
  

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
            
        case 4:
            return self.dataSource.count;
            break;
        default:
            break;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    switch (indexPath.section ) {
        case 0:
            {
                TPDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TPDetailInfoCell"];
                if(!cell){
                    cell = [[TPDetailInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TPDetailInfoCell"];
                }
                cell.detailModel = self.detailModel;
                return cell;
            }
            break;
        case 1:
        {
            TPDetailHospitalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TPDetailHospitalCell"];
            if(!cell){
                cell = [[TPDetailHospitalCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TPDetailHospitalCell"];
            }
            cell.detailModel = self.detailModel;
            return cell;
        }
            break;
            
        case 4:{
            
            TPHomeCell *cell =[tableView dequeueReusableCellWithIdentifier:@"TPHomeCell"];
            if(!cell){
                cell = [[TPHomeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TPHomeCell"];
            }
            cell.indexPath = indexPath;
            cell.model = self.dataSource[indexPath.row];
            return cell;
        }
            break;
            
        default:
            break;
    }
    
    TPProductWebViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"TPProductWebViewCell"];
    if(!cell){
        cell = [[TPProductWebViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TPProductWebViewCell"];
    }
    cell.indexPath = indexPath;
    
    YPWebViewModel *model = self.webViewModels[indexPath.section-2];
    model.webView = cell.webView;
    model.tableView = tableView;
    model.indexPath = indexPath;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 2 || indexPath.section == 3){
        YPWebViewModel *model = self.webViewModels[indexPath.section-2];
        //    DLog(@"------%@",model.html);
        return model.height;;
       
    }
    return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section > 1)
        return 60.0;
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TPDetailHeader *header = [[TPDetailHeader alloc]initWithFrame:CGRectMake(0, 0, tableView.width, [self tableView:tableView heightForHeaderInSection:section])];
    
    header.hidden = section <= 1;
    header.text = _headers[section];
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5.0f;
}


@end
